﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GrayParrot.Core.Serialization;

namespace GrayParrot.Scheduling
{
    public class JobParameters : Dictionary<string, object>
    {
        public const string INSTANCE_NAME = "@the_instance$$";
               
        public JobParameters()
        {
        }

        public JobParameters(object instance)
        {
            //if (Parameters == null)
            //{
            //    Parameters = new JobDataMap();
            //}

            SetInstance(instance);
        }

        public void Add(string key, object value)
        {
            //if (Parameters == null)
            //{
            //    Parameters = new JobDataMap();
            //}

            if (!key.Equals(JobParameters.INSTANCE_NAME, StringComparison.InvariantCultureIgnoreCase))
            {
                base.Add(key, value);
                //Parameters.Add(key.ToLower(), value);
            }
        }

        public void SetInstance(object instance)
        {
            if (this.ContainsKey(JobParameters.INSTANCE_NAME))
            {
                base[JobParameters.INSTANCE_NAME] = instance.SerializeAsXml();
                //Parameters[JobParameters.INSTANCE_NAME] = instance;
            }
            else
            {
                base.Add(JobParameters.INSTANCE_NAME, instance.SerializeAsXml());
                //Parameters.Add(JobParameters.INSTANCE_NAME, instance);
            }
        }

        public void Clear()
        {
            this.Clear();
            //this.Parameters.Clear();
        }

        public JobDataMap GetParameters()
        {
            JobDataMap retValue = new JobDataMap((IDictionary<string, object>)this);

            return retValue;
        }
    }
}
